package com.anolesoft.amljmes.fortysix.controller;

import com.anolesoft.amljmes.fortysix.dto.MesBzzPtdDto;
import com.anolesoft.amljmes.fortysix.dto.MesBzzPtjhDto;
import com.anolesoft.amljmes.fortysix.service.info.MesBzzQtfxService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("mesbzzqtfx")
public class MesBzzQtfxController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String supportingOrderNo; //配套单号
        private String dispatch; //部总装派工号
        private String fathernumber;// 父件号
        private String fathername;// 父件名称
        private String model;//机型
        private String batch;//批次
        private String sorties;//架次
        private String proportion;// 比例
        private String factoryCode;//厂代号
    }

    @Autowired
    MesBzzQtfxService mesBzzQtfxService;

    //查询未转场的配套单
    @PostMapping(value="/query",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryMesBzzQtfxInfo(MesBzzQtfxController.DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryCode(loginUser.getFacCode());

        List<MesBzzPtdDto> allList = mesBzzQtfxService.queryMesBzzQtfxInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesBzzPtdDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //查询已转场的配套单
    @PostMapping(value="/query2",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryMesBzzQjbjInfo(MesBzzQtfxController.DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryCode(loginUser.getFacCode());

        List<MesBzzPtdDto> allList = mesBzzQtfxService.queryMesBzzQjbjInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesBzzPtdDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //将配套计划状态更改为未生成
    @PostMapping(value="/updatePtjh", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateMesBzzPtjh(@RequestBody MesBzzPtjhDto inFoDtoParam){
        System.out.println(">>>>>>update");
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesBzzQtfxService.updateMesBzzPtjh(inFoDtoParam));
    }

    @PostMapping(value="/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult deleteMesBzzQtfx(@RequestBody MesBzzPtdDto inFoDtoParam){
        System.out.println(">>>>>>delete");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        inFoDtoParam.setDeletePerson(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesBzzQtfxService.deleteMesBzzQtfx(inFoDtoParam));
    }

    //将齐套分析变成配套分包
    @PostMapping(value="/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateMesBzzQtfx(@RequestBody String[] objuids){
        System.out.println(">>>>>>update");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesBzzQtfxService.updateMesBzzQtfx(asList));
    }
    //将状态变成已军检
    @PostMapping(value="/updateYjj", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateMesBzzPtfb(@RequestBody String[] objuids){
        System.out.println(">>>>>>update");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesBzzQtfxService.updateMesBzzPtfb(asList));
    }

    // 修改为未齐套
    @PostMapping(value="/updateWqt", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateIsItComplete(@RequestBody String  objuid){
        System.out.println(">>>>>>updateWqt"+objuid);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesBzzQtfxService.updateIsItComplete(objuid));
    }

    // 打印查询
    @PostMapping(value="/queryDmx",produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult querydmingxi(@RequestBody String objuid){
        objuid = objuid.substring(1,objuid.length()-1).replace("\"","'");
        System.out.println("_________________---------------------"+objuid);
        List<MesBzzPtdDto> list = mesBzzQtfxService.querydmingxi(objuid);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,list);
    }
}
